package com.team.order.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.team.order.domain.dto.OrderDTO;
import com.team.order.domain.query.OrderQuery;
import com.team.order.domain.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.team.common.log.annotation.Log;
import com.team.common.log.enums.BusinessType;
import com.team.common.security.annotation.RequiresPermissions;
import com.team.order.domain.Order;
import com.team.order.service.IAdminOrderService;
import com.team.common.core.web.controller.BaseController;
import com.team.common.core.web.domain.AjaxResult;
import com.team.common.core.utils.poi.ExcelUtil;
import com.team.common.core.web.page.TableDataInfo;

import static com.team.order.domain.query.OrderQuery.convertToQuery;

/**
 * 订单管理Controller
 * 
 * @author 梁国瑞
 * @date 2025-07-09
 */
@RestController
@RequestMapping("/admin/order")
public class AdminOrderController extends BaseController
{
    @Autowired
    private IAdminOrderService orderService;

    /**
     * 查询订单管理列表
     */
    @RequiresPermissions("order:order:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderDTO orderDTO)
    {
        // 调用分页方法
        startPage();
        // 将OrderDTO转换为Order(实体)和额外字段分别处理
        OrderQuery orderQuery = convertToQuery(orderDTO);
        // 获取分页数据
        List<OrderVO> list = orderService.selectOrderList(orderQuery);
        // 响应表分页数据
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order)
    {
        OrderQuery query = new OrderQuery();
        query.setOrder(order);
        List<OrderVO> list = orderService.selectOrderList(query);
        ExcelUtil<OrderVO> util = new ExcelUtil<OrderVO>(OrderVO.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @RequiresPermissions("order:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderService.selectOrderById(id));
    }




    /* 下面的接口是系统生成的，不要使用 */

    /**
     * 新增订单管理
     */
    @RequiresPermissions("order:order:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order)
    {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改订单表信息
     */
    @RequiresPermissions("order:order:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单管理
     */
    @RequiresPermissions("order:order:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderService.deleteOrderByIds(ids));
    }


}
